﻿////////////////////////////////////////////////////////////////////////////
//
//  Immense Engine Source File.
//  Copyright (C), Indloon 2012
// -------------------------------------------------------------------------
//  File name:   ieMathRandom.h
//  Description: Random class
//  Created:     1/12/2012 Genert Org
//  Compilers:   Visual C++ 2010 Express
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////
#ifndef __IMMENSEENGINE_MATH_RANDOM_H
#define __IMMENSEENGINE_MATH_RANDOM_H
#pragma once

/*  1. C L A S S
	=========================================================================== */

class Random {
private:
    uint seed;
	
public:
    Random( void );
	explicit Random( const uint seed );
	
	uint GetSeed( void ) const;
	void SetSeed( const uint seed );
	
	int RandInt( void );
	int RandInt( const uint max );
	int RandInt( const int min, const int max );
	
	float RandFloat( void );
	float RandFloat( const float max );
	float RandFloat( const float min, const float max );
	float CRandFloat( void );
};


/*  2. F U N C T I O N S
	=========================================================================== */
	
// -------------------------------------------------------------------------------------------------------------
ILINE Random::Random( void )
{
    this->seed = 0;
}
// -------------------------------------------------------------------------------------------------------------
ILINE Random::Random( const uint seed )
{
    this->seed = seed;
}
// -------------------------------------------------------------------------------------------------------------
ILINE uint Random::GetSeed( void ) const
{
    return ( this->seed );
}
// -------------------------------------------------------------------------------------------------------------
ILINE void Random::SetSeed( const uint seed )
{
    this->seed = seed;
}
// -------------------------------------------------------------------------------------------------------------
ILINE int Random::RandInt( void )
{
    seed = seed * 214013 + 2531011;
	
	return ( ( seed >> 16 ) & 0x7FFF );
}
// -------------------------------------------------------------------------------------------------------------
ILINE int Random::RandInt( const uint max )
{
    if( max <= 0 )
	{
		return 0;
	}

	return ( RandInt() % ( max + 1 ) );
}
// -------------------------------------------------------------------------------------------------------------
ILINE int Random::RandInt( const int min, const int max )
{
	if ( max - min <= 0 ) 
	{
		return 0;
	}

	return ( RandInt() % ( max - min + 1 ) + min );
}
// -------------------------------------------------------------------------------------------------------------
ILINE float Random::RandFloat( void )
{
	seed = seed * 214013 + 2531011;

	return ( ( ( seed >> 16 ) & 0x7FFF ) * ( 1.0f / 32767.0f ) );
}
// -------------------------------------------------------------------------------------------------------------
ILINE float Random::RandFloat( const float max ) 
{
	return ( RandFloat() * max );
}
// -------------------------------------------------------------------------------------------------------------
ILINE float Random::RandFloat( const float min, const float max )
{
	return ( RandFloat() * (max - min) + min );
}
// -------------------------------------------------------------------------------------------------------------
ILINE float Random::CRandFloat( void )
{
	return ( RandFloat() * 2.0f - 1.0f );
}
// -------------------------------------------------------------------------------------------------------------

#endif // __IMMENSEENGINE_MATH_RANDOM_H